use config::{Config, File};
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
pub struct AppConfig  {
    pub mysql: Mysql,
    pub redis: Redis,
    pub minio: Minio,
    pub meilisearch: Meilisearch,
    pub log: Log,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Log {
    pub level: String,
}



#[derive(Debug, Deserialize, Clone)]
pub struct Mysql {
    pub url: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Redis {
    pub url: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Minio {
    pub endpoint: String,
    pub access_key: String,
    pub secret_key: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct Meilisearch {
    pub url: String,
    pub api_key: Option<String>,
}


impl AppConfig  {
    pub fn load() -> Result<Self, config::ConfigError> {
        // let env = std::env::var("APP_ENV").unwrap_or("dev".into());
        Config::builder()
            .add_source(File::with_name("config/application-prod")) // 默认TOML文件
            // .add_source(File::with_name(&format!("config/{}", env)).required(false)) // 如 config/prod.toml
            .build()?
            .try_deserialize()
    }

}